import rclpy
from rclpy.node import Node
import requests
from example_interfaces.msg import String
from queue import Queue


class NovelPubNode(Node):

    def __init__(self,node_name):
        super().__init__(node_name)
        self.get_logger().info(f'{node_name} start !')
        self.novel_queue=Queue()
        self.novel_pub =self.create_publisher(String,"novel_pub",1)
        self.timer=self.create_timer(5,self.timer_callback)
        

    def timer_callback(self):
        if self.novel_queue.qsize() > 0 :
            line=self.novel_queue.get()
            msg=String()
            msg.data=line
            self.novel_pub.publish(msg)
            self.get_logger().info(f'{msg} publish !')

    def download(self,url) :
        res=requests.get(url)
        res.encoding='utf-8'
        text = res.text
        self.get_logger().info(f'download {url}，{len(text)}')
        for line in text.splitlines():
            self.novel_queue.put(line)


def main():
    rclpy.init()
    node = NovelPubNode("novel_pub")
    node.download("https://www.wenkuchina.com/lightnovel/11/171894.html")
    rclpy.spin(node)
    rclpy.shutdown()